package com.wangjiyuan.giftshuo.ui.fragment.home;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.adapter.TabPagerAdapter;
import com.wangjiyuan.giftshuo.base.BaseFragment;
import com.wangjiyuan.giftshuo.bean.home.banners.BannersBean;
import com.wangjiyuan.giftshuo.bean.home.preset.ChannelsBean;
import com.wangjiyuan.giftshuo.bean.home.secondarybanners.SecondaryBannersBean;
import com.wangjiyuan.giftshuo.ui.fragment.other.search.SearchFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class NavFragment extends BaseFragment implements NavContract.NavView, View.OnClickListener {


    private TabLayout tab_layout;
    private LinearLayout seach;
    private ImageView alarm_clock;
    private ViewPager content_pager;
    private ImageView index;
    private Intent intent;
    private PopupWindow tab_popupwidow;
    private GridLayout grid_layout;
    private TextView textView;
    public static final String HOT_WORD = "hot_word";
    List<SecondaryBannersBean> sblist;
    List<BannersBean> blist;
    List<ChannelsBean> clist;
    Bundle arguments;

    public NavFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        arguments = getArguments();
        sblist = (List<SecondaryBannersBean>) arguments.getSerializable("ad");
        blist = (List<BannersBean>) arguments.getSerializable("ba");
        clist = (List<ChannelsBean>) arguments.getSerializable("pr");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_nav, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tab_layout = (TabLayout) view.findViewById(R.id.tab_layout);
        textView = (TextView) view.findViewById(R.id.textView);
        seach = (LinearLayout) view.findViewById(R.id.search);
        alarm_clock = (ImageView) view.findViewById(R.id.alarm_clock);
        index = (ImageView) view.findViewById(R.id.index);
        content_pager = (ViewPager) view.findViewById(R.id.content_pager);
        tab_layout.setupWithViewPager(content_pager);
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < clist.size(); ++i) {
            RecyclerFragment fragment = new RecyclerFragment();
            Bundle bundle = (Bundle) arguments.clone();

            bundle.putInt("id", clist.get(i).getId());
            fragment.setArguments(bundle);

            fragments.add(fragment);
        }
        MyPopupWindow();
        TabPagerAdapter adapter = new TabPagerAdapter(getActivity().getSupportFragmentManager(),
                fragments, clist);
        content_pager.setAdapter(adapter);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        seach.setOnClickListener(this);
        alarm_clock.setOnClickListener(this);
        index.setOnClickListener(this);
    }


    @Override
    public void search(List<String> searchhot) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("hot", (Serializable) searchhot);
        SearchFragment searchFragment = new SearchFragment();
        searchFragment.setArguments(bundle);
        getActivity().getSupportFragmentManager().beginTransaction()
                .add(R.id.all, searchFragment, null)
                .commit();
    }

    @Override
    public void alarm() {
        intent = new Intent();
    }

    @Override
    public void index() {
        if (!tab_popupwidow.isShowing()) {
            for (int i = 0; i < clist.size(); i++) {
                ((TextView) grid_layout.getChildAt(i)).setTextColor(Color.BLACK);
            }
            int selectedTabPosition = tab_layout.getSelectedTabPosition();
            ((TextView) grid_layout.getChildAt(selectedTabPosition)).setTextColor(Color.RED);
            tab_layout.setVisibility(View.INVISIBLE);
            textView.setVisibility(View.VISIBLE);
            tab_popupwidow.showAsDropDown(tab_layout);
            index.setImageResource(R.mipmap.arrow_index_up);
        } else {
            tab_popupwidow.dismiss();
        }
    }

    /**
     * 用于创建popupwindow
     */
    private void MyPopupWindow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow, null);
        tab_popupwidow = new PopupWindow(view,
                GridLayout.LayoutParams.MATCH_PARENT, GridLayout.LayoutParams.WRAP_CONTENT, true);
        tab_popupwidow.setTouchable(true);
        tab_popupwidow.setOutsideTouchable(true);
        tab_popupwidow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        grid_layout = (GridLayout) view.findViewById(R.id.grid_layout);
        int widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;

        for (int i = 0; i < (clist.size() / 4 + 1) * 4; ++i) {
            final TextView txt = new TextView(mContext);
            GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
            layoutParams.setMargins(2, 2, 2, 2);
            layoutParams.setGravity(Gravity.CENTER);
            layoutParams.width = widthPixels / 4 - 4;
            layoutParams.height = layoutParams.width / 2;
            txt.setLayoutParams(layoutParams);
            if (i < clist.size()) {
                txt.setText(clist.get(i).getName());
                txt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (int i = 0; i < clist.size(); i++) {
                            if (!grid_layout.getChildAt(i).equals(v)) {
                                ((TextView) grid_layout.getChildAt(i)).setTextColor(Color.BLACK);
                            } else {
                                content_pager.setCurrentItem(i);
                            }
                        }
                        ((TextView) v).setTextColor(Color.RED);

                        tab_popupwidow.dismiss();
                    }
                });
            }
            txt.setTag(i);
            txt.setGravity(Gravity.CENTER);
            txt.setBackgroundColor(Color.WHITE);
            grid_layout.addView(txt);

        }
        tab_popupwidow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                index.setImageResource(R.mipmap.arrow_index_down);
                textView.setVisibility(View.INVISIBLE);
                tab_layout.setVisibility(View.VISIBLE);
            }
        });
    }

    /**
     * 点击监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search:
                NavPresenter presenter = new NavPresenter(this);
                presenter.getHotWord();

                break;
            case R.id.alarm_clock:
                alarm();
                break;
            case R.id.index:
                index();
                break;
        }
    }
}
